﻿CloudDisk.FileList = function(){
	this.data = [];
	this.selected = [];
	this.setView();
};

CloudDisk.FileList.prototype = {

    render: function(data){
	    var icons = CloudDisk.Setting.Icons;
		var iconPath = CloudDisk.Setting.ICONS_PATH;
	    this.data = data.concat();
		$.each(this.data, function(n, v){
		    if(v.isFolder){
			    v.smallIcon = iconPath + icons.folder.small;
				v.largeIcon = iconPath + icons.folder.large;
			} else {
			    var icon = CloudDisk.Util.getIconByMimeType(v.mime_type);
			    v.smallIcon = icon.small;
				v.largeIcon = icon.large;
			}
		});
	    this.view.render(data);
	},
	
	setView: function(type){
	    var that = this;
		if(this.view){
	        this.view.hide();
		}
	    this.view = CloudDisk.ViewManager.getView(type);
		if(this.view.eventListeners.length < 1){	   
		    this.view.on('select', function(e, id){
			    var item = that.getById(id);
				item.selected = true;
			    that.selected.push(item);
			    that.onSelect();
			});
			this.view.on('unselect', function(e, id){
				for(var i = 0; i < that.selected.length; ++i){
				    if(that.selected[i].file_id == id)break;
				}
				that.getById(id).selected = false;
				that.selected.splice(i - 1, 1).selected = false;
			    that.onUnselect();
			});
			this.view.on('open', function(e, id){
				$.each(that.data, function(n, v){
					if(v.file_id + '' == id){
						that.onOpen(v);
					}
					return false;
				});            
			});
			this.view.on('createfolder', function(e){
            
			});
		}
		this.view.show();
		this.refresh();
	},
	
	//create a blank file item which will be added soon
	createBlankFile: function(){
	    this.view.createBlankItem();
	},
	
	addFile: function(id){
	
	},
	
	removeFile: function(id){
	    
	},
	
	refresh: function(){
	    this.view.render(this.data);
	},
	
	getView: function(){
	    return this.view;
	},
	
	getSelections: function(){
	    return this.selected;
	},
	
	getById: function(id){
        var item;
	    $.each(this.data, function(n, v){
		    if(v.file_id == id){
			    item = v;
			    return false;
			}
		});
		return item;
	},
    
	selectAll: function(){
	    $.each(this.data, function(n, v){
		    v.selected = true;
		});
        this.selected = this.data.concat();
		this.refresh();
	},
	
	clearSelections: function(){
	    $.each(this.data, function(n, v){
		    v.selected = false;
		});
	    this.selected = [];
		this.refresh();
	},
	
	onSelect: function(){

	}
};

CloudDisk.ViewManager = (function(){
    var el = $('#file_list .tbody');
    var listView = null;
	var gridView = null;
	
	return {
	    //default is ListView
	    getView: function(type){
		    if(type == 'grid'){
			    if(!gridView){
				    gridView = new CloudDisk.GridView({
					    ct: el,
						itemTpl: $("#gridViewTpl")
					});
				}
				return gridView; 
			} else {
			    if(!listView){
				    listView = new CloudDisk.ListView({
					    ct: el,
						itemTpl: $("#listViewTpl")
					});
				}
				return listView; 
			}
		}
	};
})();